import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome(r'./chromedriver.exe')

driver.get('http://10.11.8.83')
driver.maximize_window()


# driver.find_element_by_name('tj_trnews').click()
# driver.get('https://www.baidu.com')
# driver.find_element_by_name('tj_trhao123').click()

# navs = driver.find_elements_by_class_name('mnav')
# for j in navs:
#     print(j)
#
# for index in range(len(navs)):
#     navs[index].click()
#     driver.back()
#     driver.refresh()
#     driver.forward()
#     driver.maximize_window()
#     driver.minimize_window()
#     driver.maximize_window()


def login():
    name_input = driver.find_element_by_id('username')
    password_input = driver.find_element_by_id('password')
    username = '2018329621053'
    password = '000000'
    name_input.clear()
    for i in username:
        # time.sleep(0.03)
        name_input.send_keys(i)
    for j in password:
        # time.sleep(0.03)
        password_input.send_keys(j)
    driver.find_element_by_xpath('//*[@id="LoginForm"]/table/tbody/tr[3]/td[2]/input').click()
    # time.sleep(0.5)
    # driver.get('http://10.11.8.83/login/hpindex_student.php')
    driver.refresh()
    courses = driver.find_elements_by_xpath(
        '//*[@id="BookClassDIV"]/table/tbody/child::tr/td[2]/ul/li/a')
    for course in courses:
        if '读写3' in course.text:
            # print(type(course.get_attribute('href')))
            link = course.get_attribute('href')
            print(link[link.find('BookID=') + 7:link.find('BookID=') + 10])
            print(course.get_attribute('href'))
            print(course.text)
            # driver.get('http://10.11.8.83/book/book{}/index.php?Quiz=N&whichActionPage='.format(course.text))
            course.click()
            driver.refresh()
            return link[link.find('BookID=') + 7:link.find('BookID=') + 10]
    else:
        print('未找到相关课程！')
        return


def write_understanding(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    url = 'http://10.11.8.83/book/book{}/uyu64mc.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 6, 4)
    driver.get(url)
    driver.find_element_by_id('TR_0')
    for i in range(8):
        # print('//*[@id="TR_' + str(i) + '"]/td[2]/input')
        inputs = driver.find_elements_by_xpath('//*[@id="TR_' + str(i) + '"]/td[2]/input')
        # //*[@id="TR_0"]/td[1]/input
        for item in inputs:
            if item.get_attribute('value') == answer[i]:
                item.click()
                break
        # time.sleep(0.3)  # 每做一题休息半秒
        inputs = driver.find_elements_by_xpath('//*[@id="TR_' + str(i) + '"]/td[1]/input')
        for item in inputs:
            if item.get_attribute('value') == answer[i]:
                item.click()
                break
    # driver.find_element_by_xpath("//div[contains(@class,'submit')]/a").click()
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass
    # time.sleep(1)


def a_write_word_in_use(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    url = 'http://10.11.8.83/book/book{}/uyu27blank.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 2, 7)
    driver.get(url)
    for i in range(10):
        blank = driver.find_element_by_id('Blank_' + str(i) + '_0')
        blank.clear()
        for letter in answer[i]:
            blank.send_keys(letter)
            # time.sleep(0.03)
        # time.sleep(0.1)
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    # tip = driver.find_element_by_xpath("/html/body/div[3]/div[2]/p[1]")
    # if 'Done with this activity' not in tip.text:
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass
    # time.sleep(1)


def a_word_building_practice_one(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    url = 'http://10.11.8.83/book/book{}/uyu29blank.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 2, 9)
    driver.get(url)
    for i in range(12):
        blank = driver.find_element_by_id('Blank_' + str(i) + '_0')
        blank.clear()
        for letter in answer[i]:
            blank.send_keys(letter)
            # time.sleep(0.03)
        # time.sleep(0.3)
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    # tip = driver.find_element_by_xpath("/html/body/div[3]/div[2]/p[1]")
    # if 'Done with this activity' not in tip.text:
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass
    # time.sleep(1)


def a_word_building_practice_two(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    url = 'http://10.11.8.83/book/book{}/uyu27blank.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 2, 10)
    driver.get(url)
    for i in range(12):
        blank = driver.find_element_by_id('Blank_' + str(i) + '_0')
        blank.clear()
        for letter in answer[i]:
            blank.send_keys(letter)
            # time.sleep(0.03)
        # time.sleep(0.3)
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    # tip = driver.find_element_by_xpath("/html/body/div[3]/div[2]/p[1]")
    # if 'Done with this activity' not in tip.text:
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass
    # time.sleep(1)


def a_blanked_close(book_name, unit, answer):
    url = "http://10.11.8.83/book/book{}/uyu211drop.php?UnitID={}&SectionID={}&SisterID={}".format(book_name, unit, 2, 11)
    driver.get(url)
    for i in range(len(answer)):
        # 起点
        start = driver.find_element_by_xpath('//*[@id="list_' + str(ord(answer[i]) - ord('A') + 1) + '"]')
        # 终点
        end = driver.find_element_by_xpath('//*[@id="Item_0_' + str(i) + '"]')
        actions = ActionChains(driver)
        actions.click_and_hold(start)
        actions.drag_and_drop(start, end)
        # 执行
        actions.perform()
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass


def a_expressions_in_use(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    url = 'http://10.11.8.83/book/book{}/uyu27blank.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 2, 12)
    driver.get(url)
    for i in range(8):
        blank = driver.find_element_by_id('Blank_' + str(i) + '_0')
        blank.clear()
        for letter in answer[i]:
            blank.send_keys(letter)
            # time.sleep(0.03)
        # time.sleep(0.1)
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    # tip = driver.find_element_by_xpath("/html/body/div[3]/div[2]/p[1]")
    # if 'Done with this activity' not in tip.text:
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass
    # time.sleep(1)


def write_word_in_use(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    url = 'http://10.11.8.83/book/book{}/uyu27blank.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 6, 6)
    driver.get(url)
    for i in range(10):
        blank = driver.find_element_by_id('Blank_' + str(i) + '_0')
        blank.clear()
        for letter in answer[i]:
            blank.send_keys(letter)
            # time.sleep(0.03)
        # time.sleep(0.1)
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    # tip = driver.find_element_by_xpath("/html/body/div[3]/div[2]/p[1]")
    # if 'Done with this activity' not in tip.text:
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass
    # time.sleep(1)


def write_expressions_in_use(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    # 注意这个地方可能出现某一题有两个空，要考虑修改一下
    url = 'http://10.11.8.83/book/book{}/uyu27blank.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 6, 7)
    driver.get(url)
    for i in range(8):
        blank = driver.find_element_by_id('Blank_' + str(i) + '_0')
        blank.clear()
        for letter in answer[i]:
            blank.send_keys(letter)
            time.sleep(0.03)
        time.sleep(0.1)
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    # tip = driver.find_element_by_xpath("/html/body/div[3]/div[2]/p[1]")
    # if 'Done with this activity' not in tip.text:
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass


def element_dragging(book_name, unit, answer):
    url = "http://10.11.8.83/book/book{}/uyu72drag.php?UnitID={}&SectionID={}&SisterID={}".format(book_name, unit, 7, 2)
    driver.get(url)
    # # 起点
    # start = driver.find_element_by_xpath('//*[@id="firstlist"]/li[1]')
    # # 终点
    # end = driver.find_element_by_xpath('//*[@id="firstlist"]/li[5]')
    block1 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[1]')
    block2 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[2]')
    block3 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[3]')
    block4 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[4]')
    block5 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[5]')
    block6 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[6]')
    block7 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[7]')
    block8 = driver.find_element_by_xpath('//*[@id="firstlist"]/li[8]')
    ActionChains(driver).drag_and_drop_by_offset(block1, 0, 50).perform()
    time.sleep(1.5)
    ActionChains(driver).drag_and_drop_by_offset(block1, 0, 50).perform()
    time.sleep(1.5)
    ActionChains(driver).drag_and_drop_by_offset(block1, 0, 50).perform()
    time.sleep(1.5)
    ActionChains(driver).drag_and_drop_by_offset(block1, 0, 50).perform()
    time.sleep(1.5)
    ActionChains(driver).drag_and_drop_by_offset(block2, 0, 50).perform()
    time.sleep(1.5)
    # print(start.text)
    # draggable = driver.find_element_by_xpath('//*[@id="Item_1"]')
    # ActionChains(driver).drag_and_drop_by_offset(start, 0, 50).perform()
    # ActionChains(driver).drag_and_drop_by_offset(start, 0, 50).perform()
    # actions = ActionChains(driver)
    # actions.click_and_hold(start)
    # actions.drag_and_drop(start, end)
    # # 执行
    # actions.perform()
    time.sleep(30)


def collection_practice_one(book_name, unit, answer):
    # answer应该是一个存储有答案的字典
    url = 'http://10.11.8.83/book/book{}/uyu72blank.php?UnitID={}&SectionID={}&SisterID={}'.format(book_name, unit, 7, 2)
    driver.get(url)
    for i in range(3):
        for j in range(3):
            blank = driver.find_element_by_id('Blank_' + str(i) + '_' + str(j) + '')
            blank.clear()
            for letter in answer[i][j]:
                blank.send_keys(letter)
                # time.sleep(0.03)
            # time.sleep(0.3)
    driver.find_element_by_xpath("//div[contains(@class,'submit')]").click()
    # tip = driver.find_element_by_xpath("/html/body/div[3]/div[2]/p[1]")
    # if 'Done with this activity' not in tip.text:
    try:
        driver.find_element_by_class_name('layui-layer-btn0').click()
    except:
        pass
    # time.sleep(1)


book = login()
a_write_word_in_use_keys = [['whereby', 'pursuit ', 'inhibit', 'maintain', 'patriotic', 'transcended ', 'endeavors', 'dedication', 'prestige', 'nominate']]
a_word_building_practice_one_keys = [
    ['inhabit', 'participant', 'attendant', 'pollutant', 'descendant', 'contestant', 'tolerant', 'resultant', 'neglectful', 'resourceful', 'boastful',
     'respectful']]
a_word_building_practice_two_keys = [
    ['resultant', 'tolerant', 'pollutants', 'inhabited', 'contestants', 'descendants', 'attendants', 'respectful', 'participants', 'neglectful', 'resourceful',
     'boastful']]
a_blanked_close_keys = [['F', 'G', 'H', 'J', 'E', 'A', 'N', 'I', 'K', 'M']]
a_expressions_in_use_keys = [
    ['removed from', 'failed in', 'in the pursuit of', 'deviated from', 'precludes', 'triumph over', 'work their way into', 'written off']]
write_understanding_keys = [['c', 'b', 'a', 'd', 'd', 'b', 'c', 'd']]
write_word_in_use_keys = [['indulge', 'propelling', 'aggravated', 'dazzled', 'alleviating', 'renowned', 'eloquent', 'destined', 'scorned', 'applause']]
write_expressions_in_use_keys = [['up', 'in', 'on', 'up', 'to', 'on', 'as', 'out']]
collection_practice_one_keys = [[['repeated', 'overwhelming', 'immense'], ['heroic', 'sound', 'substantial'], ['attained', 'fueled', 'achieve']]]
for k in range(1, 2):
    a_write_word_in_use(book, k, a_write_word_in_use_keys[k-1])
    a_word_building_practice_one(book, k, a_word_building_practice_one_keys[k - 1])
    a_word_building_practice_two(book, k, a_word_building_practice_two_keys[k - 1])
    a_blanked_close(book, k, a_blanked_close_keys[k - 1])
    a_expressions_in_use(book, k, a_expressions_in_use_keys[k - 1])
    write_understanding(book, k, write_understanding_keys[k-1])
    write_word_in_use(book, k, write_word_in_use_keys[k-1])
    write_expressions_in_use(book, k, write_expressions_in_use_keys[k-1])
    collection_practice_one(book, k, collection_practice_one_keys[k - 1])
